var ProductModule = function () {
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	"autoWidth":false,
                    "scrollX": true,
                	"lengthMenu": [
                                   [10, 15, 20, 50],
                                   [10, 15, 20, 50]
                               ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                     },
                     "columnDefs": [
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [0]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [1]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [2]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [3]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [4]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [5]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [6]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [7]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [8]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [9]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [10]
            						}
                                ],
                     "columns": [{ "data": "productCode" },
                                { "data": "briefName" },
                                { "data": "duration" },
                                { "data": "prospectiveEarnings"},
                                { "data": "incomeType" },
                                { "data": "scale" },
                                { "data": "available" },
                                { "data": "state" },
                                { "data": "orders" },
                                { "data": "auditState" }],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/publisher/product/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   
                	   $('td:eq(2)', nRow).html(aData.duration+"&nbsp;月");
                	   $('td:eq(5)', nRow).html(formatCurrency(aData.scale)+"&nbsp;元");
                	   $('td:eq(6)', nRow).html(formatCurrency(aData.available)+"&nbsp;元");
                	   var incomeType;
                	   if(aData.incomeType == 0){
                		   incomeType = "固定";
                	   }else if(aData.incomeType == 1){
                		   incomeType = "浮动";
                	   }else if(aData.incomeType == 2){
                		   incomeType = "固定+浮动";
                	   }
                	   $('td:eq(4)', nRow).html(incomeType);
                	   
                	   var state;
                	   if(aData.state == 0){
                		   state = "即将上线";
                	   }else if(aData.state == 1){
                		   state = "正在募集中";
                	   }else if(aData.state == 2){
                		   state = "募集完成";
                	   }
                	   $('td:eq(7)', nRow).html(state);
                	   
                	   $('td:eq(9)', nRow).html(aData.auditState==1?"已审核":"未审核");
                	   
                	   var opera = '<a class="orders fa fa-bars fa-lg" title="查看订单" href="javascript:;"></a>';
                	   $('td:eq(10)', nRow).html(opera);
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   
                	   var result = {};
            	       var params = (window.location.search.split('?')[1] || '').split('&');
            	       for(var param in params) {
            	    	   if (params.hasOwnProperty(param)) {
	            	            paramParts = params[param].split('=');
	            	            result[paramParts[0]] = decodeURIComponent(paramParts[1] || "");
            	    	   }
            	       }
                	   
            	       if(result.productGroupId != undefined){
            	    	   //$('#productGroupId').options[result.productGroupId].selected = true;
            	    	   
	           	    	    var se1 = $('#productGroupId')[0];
	           	    		var i=0;
	           	    		var lenth = se1.length;
	           	    	    for(;i<lenth;i++) {
	           	    	    	if(se1[i].value == result.productGroupId){
	           	    	    		se1[i].selected = true;
	           	    	    	}
	           	    	    }
            	       }
            	       
                	   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + ',"orderBy":"create_time desc"' +"}";
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                
                $('#modelEditable').on("click", "a.orders", (function (e) {
                        e.preventDefault();

                        var nRow = $(this).parents('tr')[0];
                        var id = nRow.children[0].id;
                    	
                        window.location.href = "publisher/orders/list/"+id;
                }));
                
                $('#queryBtn').click(function(){
                	oTable.fnClearTable();
                });
            }

        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();